package g2.routemaster.gui.actions;

import g2.routemaster.gui.GManager;
import g2.routemaster.model.GameManager;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

import de.tu_bs.coobra_plugin.CoObRAPersistentObject;

public class SaveGameAction extends Action {
   private ApplicationWindow window;
   public SaveGameAction(ApplicationWindow window) {
      setText("Save Game");
      setToolTipText("Save Game");
      this.window = window;
   }

   public void run() {
      FileDialog dial = new FileDialog(window.getShell(), SWT.SAVE);
      String path = dial.open();
      if(path != null){
         GameManager gm = GManager.instance().getGameManager();     	
         saveGameManager(gm, path);
      }
   }

   public void saveGameManager(GameManager gm, String path) {
      CoObRAPersistentObject.getVMRepository().setRootObject(gm);
      try {
         CoObRAPersistentObject.getVMRepository().compact();
         CoObRAPersistentObject.getVMRepository().store(path);
         System.out.println("Successful save");
      } catch (Exception e) {
         System.out.println("Error on game save");
         e.printStackTrace();
      }
   }   
}
